#include <iostream>
#include <Windows.h>
#include <thread>
//#include <process.h>
//#include <cstdio>
//#include <chrono>
using namespace std;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
// 任务栏图标的唯一标识符
#define MY_TRAY_ICON_ID 1001
//unsigned WINAPI
void messageLoop(
//        void* arg
) {
    MSG msg;
    while (GetMessage(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
//unsigned WINAPI
void coutdownTime(
//        void* arg
) {
    const int bigcountdown = 2400;
    const int littleCountdown = 300;
    int countdown = bigcountdown;
    while (true) {
//        fflush(stdout);
//        this_thread::sleep_for(chrono::seconds(bigcountdown));
//        Sleep(bigcountdown*1000);
//        cout << countdown << endl;
        time_t startTime = time(NULL);
        while (time(NULL) - startTime < 1) {
            // Busy wait for 1 second
        }
        countdown--;
        if (countdown <= 0) {
            int result = MessageBox(nullptr, TEXT("Yes: rest now\nNo: 5 minutes"), TEXT("\tTime to rest"), MB_YESNO);
            if (result == IDYES) {
                cout << "rest now" << endl;
                system("rest.hta");
//                Sleep(bigcountdown*1000);
//            this_thread::sleep_for(chrono::seconds(bigcountdown));
                countdown = bigcountdown;
            } else if (result == IDNO) {
                cout << "5 minutes" << endl;
//            this_thread::sleep_for(chrono::seconds(littleCountdown));
//                Sleep(littleCountdown*1000);
                countdown = littleCountdown;
            }
        }
    }
}
int main() {
    // 创建一个窗口类
    WNDCLASSEX wc = {0};
    wc.cbSize = sizeof(WNDCLASSEX);
//    wc.lpfnWndProc = WindowProc;
    wc.hInstance = GetModuleHandle(nullptr);
    wc.lpszClassName = TEXT("MyHiddenWindowClass");
    RegisterClassEx(&wc);
    // 创建一个隐藏的窗口
    HWND hwnd = CreateWindowEx(0, TEXT("MyHiddenWindowClass"), TEXT("Hidden Window"), 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
    // 设置任务栏图标
    NOTIFYICONDATA nid = {0};
    nid.cbSize = sizeof(NOTIFYICONDATA);
    nid.hWnd = hwnd;
    nid.uID = MY_TRAY_ICON_ID;
    nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
    nid.uCallbackMessage = WM_USER + 1;
    nid.hIcon = LoadIcon(nullptr, IDI_INFORMATION);
    time_t result = time(nullptr);
    char *timeString = ctime(&result);
    lstrcpy(nid.szTip, TEXT(timeString));
    Shell_NotifyIcon(NIM_ADD, &nid);
//    coutdownTime();
//    _beginthreadex(NULL, 0, messageLoop, NULL, 0, NULL);
//    _beginthreadex(NULL, 0, coutdownTime, hwnd, 0, NULL);
//    system("pause");
    thread t(messageLoop);
    thread t2(coutdownTime);
    t.join();
    t2.join();
//    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) messageLoop, NULL, 0, NULL);
    // 消息循环
    // 移除任务栏图标
    Shell_NotifyIcon(NIM_DELETE, &nid);
    return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    if (uMsg == WM_USER + 1) {
        HMENU hMenu;
        switch (lParam) {
            case WM_RBUTTONDOWN:
            case WM_LBUTTONDOWN:
            case WM_LBUTTONDBLCLK:
            case WM_RBUTTONDBLCLK:
            case WM_CONTEXTMENU:
            case WM_MOUSEHOVER: {
                hMenu = CreatePopupMenu();
                time_t result = time(nullptr);
                char *timeString = ctime(&result);
                AppendMenu(hMenu, MF_STRING, 1, TEXT("show"));
                AppendMenu(hMenu, MF_STRING, 2, TEXT(timeString));
                AppendMenu(hMenu, MF_STRING, 3, TEXT("exit"));
                POINT pt;
                GetCursorPos(&pt);
                int cmd = TrackPopupMenu(hMenu, TPM_RETURNCMD | TPM_NONOTIFY, pt.x, pt.y, 0, hwnd, nullptr);
                DestroyMenu(hMenu);
                if (cmd == 1) {
                    int messageBox = MessageBox(nullptr, TEXT("Yes: rest now\nNo: 5 minutes"), TEXT("\tTime to rest"), MB_YESNO);
                    if (messageBox == IDYES) {
                        cout << "rest now" << endl;
                        system("rest.hta");
                    } else if (messageBox == IDNO) {
                        cout << "5 minutes" << endl;
                    }
                } else if (cmd == 3) {
                    PostMessage(hwnd, WM_CLOSE, 0, 0);
                    abort();
//                    exit(1);
//                    return 1;
                }
            }
            default:
                break;
        }
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}